package com.example.wangkai.wangkai.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wangkai.wangkai.R;
import com.example.wangkai.wangkai.app.MyApplication;
import com.example.wangkai.wangkai.presenter.LoginPresenter;
import com.example.wangkai.wangkai.utils.GlobalConsts;
import com.example.wangkai.wangkai.view.ILoginView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class MainActivity extends AppCompatActivity implements ILoginView{
    @ViewInject(R.id.et_username)
    private EditText edusername;
    @ViewInject(R.id.et_password)
    private EditText edpassword;
    @ViewInject(R.id.btn_login)
    private Button  btnlogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);

        //注入控件   初始化
        x.view().inject(this);
    }

    public void doClick(View v){
        switch (v.getId()) {
            case (R.id.btn_login):
                Log.e("main","login"+TextUtils.isEmpty(edusername.getText().toString()));
                String name=edusername.getText().toString();
                String pwd=edpassword.getText().toString();
                if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(MainActivity.this,"请输入账号或密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                login(name,pwd);
                break;
        }
    }

    private void login( String name, String pwd) {
        RequestParams params = new RequestParams(GlobalConsts.URL_LOAD_Login);
        params.addBodyParameter("phone",name);
        params.addBodyParameter("password",pwd);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Log.e("s","s=-==="+s);
                try {
                    JSONObject json = new JSONObject(s);

                    if(json.getInt("code")==1){
                        // TODO: 2016/11/9 登录成功
                        Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(MainActivity.this,DetialActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(MainActivity.this,json.getString("message"),Toast.LENGTH_SHORT).show();
                    }
                    String local = json.getJSONObject("data").getString("token");
                    MyApplication app=MyApplication.getContext();
                    app.saveToken(local);
//                    MyApplication..getJSONObject("data").getString("token");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Log.e("main","throwa"+throwable.toString());
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void loginSuccess() {
        Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginFailed(String errorMessage) {
        Toast.makeText(MainActivity.this,"登录失败"+errorMessage,Toast.LENGTH_SHORT).show();
    }
}
